package utils;

import java.awt.Font;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Scanner;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;

public class BarChart{
	
	static int num1;
	static int num2;
	static int num3;
	static int num4;

	public static void drawPicture(String course) {
		String sql = "select * from students ";
		JFreeChart chart=null;
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		ArrayList<Double> scoreList = new ArrayList<>();

		try {
			con = JDBCUtil.getConnection();
			ps = con.prepareStatement(sql);
			rs = ps.executeQuery();
			while (rs.next()) {
				scoreList.add(rs.getDouble(course));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String name="D://"+course+".jpg";
		for (int i = 0; i < scoreList.size(); i++) {
			if (scoreList.get(i) >= 60 && scoreList.get(i) < 70) {
				num1++;
			}
			else if (scoreList.get(i) >= 70 && scoreList.get(i) < 80) {
				num2++;
			}
			else if (scoreList.get(i) >= 80 && scoreList.get(i) < 90) {
				num3++;
			}
			else if (scoreList.get(i) >= 90) {
				num4++;
			}
		}
		CategoryDataset dataset = getDataSet();
		chart = ChartFactory.createBarChart3D(course+"成绩", // 图表标题
				"分数段", // 目录轴的显示标签
				"人数", // 数值轴的显示标签
				dataset, // 数据集
				PlotOrientation.VERTICAL, // 图表方向：水平、垂直
				true, // 是否显示图例(对于简单的柱状图必须是false)
				false, // 是否生成工具
				false // 是否生成URL链接
				);
		CategoryPlot plot = chart.getCategoryPlot();// 获取图表区域对象
		CategoryAxis domainAxis = plot.getDomainAxis(); // 水平底部列表
		domainAxis.setLabelFont(new Font("黑体",Font.BOLD,14)); // 水平底部标题
		domainAxis.setTickLabelFont(new Font("宋体",Font.BOLD,12)); // 垂直标题
		ValueAxis rangeAxis = plot.getRangeAxis();// 获取柱状
		rangeAxis.setLabelFont(new Font("黑体",Font.BOLD,15));chart.getLegend().setItemFont(new Font("黑体",Font.BOLD,15));chart.getTitle().setFont(new Font("宋体",Font.BOLD,20));// 设置标题字体
		

		FileOutputStream out = null;
		try
		{
			out = new FileOutputStream(name);
			ChartUtilities.writeChartAsJPEG(out, 0.5f, chart, 400, 300, null);num1=num2=num3=num4=0;
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				out.close();
			} catch (Exception ex) {
				ex.printStackTrace();
			}
		}
}
	private static CategoryDataset getDataSet() {
		DefaultCategoryDataset dataset = new DefaultCategoryDataset();

		dataset.addValue(num1, "60-70", "60-70");
		dataset.addValue(num2, "70-80", "70-80");
		dataset.addValue(num3, "80-90", "80-90");
		dataset.addValue(num4, "90-100", "90-100");

		return dataset;

	}

	
	


}
